define(['underscore', 'jquery', 'bootstrap'], function(_, $) {
  return menu;

  function menu(data) {
    var $main = $('#menu').addClass('bs-docs-sidebar hidden-print hidden-xs hidden-sm').html('');
    var $ul = $('<ul class="nav bs-docs-sidenav">');
    var current = location.hash.replace('#/', '').split('/');
    _.map(data, function(types, clazz) {
      var $li = $('<li>');
      if (current[0] === clazz) {
        $li.addClass('active');
        window.$env.$li = $li;
      }

      $li.append(
        $('<a>')
          .attr('href', 'javascript:;')
          .text(clazz)
          .click(function() {
            $('#menu li').removeClass('active');
            window.$env.$li = $(this).blur().parent().addClass('active');
          })
      );
      var $subUl = $('<ul class="nav">');
      _.map(types, function(type, index) {
        var $subLi = $('<li>');
        if (current[0] === clazz && current[1] === type) {
          $subLi.addClass('active');
          window.$env.$subLi = $subLi;
        }

        $subUl.append(
          $subLi.append(
            $('<a>')
              .attr('href', '#/' + clazz + '/' + type)
              .text(type)
              .click(function() {
                $(this).parent().parent().children('li').removeClass('active');
                window.$env.$subLi = $(this).blur().parent().addClass('active');
              })
          )
        )
      })
      $li.append($subUl);
      $ul.append($li);
      $main.append($ul);
    })

    $(".bs-docs-container [href=#]").click(function(e) {
      e.preventDefault()
    })

    $main.affix({
      offset: {
        top: 1
      }
    });
  } 
})